package com.ilz.publictemplatedemo.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import com.ilz.publictemplatedemo.R;
import com.ilz.publictemplatedemo.base.BaseActivity;
import com.ilz.publictemplatedemo.utils.Tools;

import java.lang.ref.WeakReference;

public class SplashActivity extends BaseActivity {

    private static final int COUNT_TIME_DOWN = 1001;
    private int count = 3;
    private TextView countText;

    @Override
    protected int initLayout() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initData() {

        Log.e("test", "ClassName--->" + this.getClass().getName());
        Log.e("test", "HashKey----->" + Tools.getHashKey(this));
        countText = findViewById(R.id.countText);
        countText.setText(String.valueOf(count));
        MyHandler handler = new MyHandler(this);
        handler.sendEmptyMessageDelayed(COUNT_TIME_DOWN, 1000);

    }

    @Override
    protected void initListener() {

    }

    private static class MyHandler extends Handler {
        private WeakReference<SplashActivity> splashActivity;

        MyHandler(SplashActivity activity) {
            splashActivity = new WeakReference<>(activity);
        }

        @SuppressLint("StringFormatInvalid")
        @Override
        public void handleMessage(Message msg) {
            final SplashActivity activity = splashActivity.get();
            if (activity != null) {
                if (msg.what == COUNT_TIME_DOWN) {
                    activity.count--;
                    if (activity.count > 0) {
                        activity.countText.setText(String.valueOf(activity.count));
                        sendEmptyMessageDelayed(COUNT_TIME_DOWN, 1000);
                    } else {
                        activity.startActivity(new Intent(activity, HomeActivity.class));
                        activity.finish();
                    }
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
